package com.simple.datax.dao.impl;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.simple.base.dao.BaseDAO;
import com.simple.datax.dao.IVwDataxEnterpriseDAO;
import com.simple.datax.vo.VwDataxEnterpriseVO;
import com.simple.util.common.StringUtil;
import com.simple.util.object.PageInfoBean;

@Repository("vwDataxEnterpriseDAO")
public class VwDataxEnterpriseDAOImpl extends BaseDAO implements IVwDataxEnterpriseDAO {

	@Override
	public VwDataxEnterpriseVO findById(String enCode) {

		return (VwDataxEnterpriseVO)super.get(VwDataxEnterpriseVO.class, enCode);
	}

	@Override
	public List<VwDataxEnterpriseVO> findAll() {
		return super.query(VwDataxEnterpriseVO.class);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<VwDataxEnterpriseVO> findListByPage(String ecCode,
			String ecName, PageInfoBean pageInfoBean) {
		final Map<String, Object> paramMap = new LinkedHashMap<String,Object>();
        final StringBuffer sb = new StringBuffer(500);
        
        sb.append(" select a from VwDataxEnterpriseVO a where 1>0 " );
        if(!StringUtil.isNull(ecCode)){
            sb.append( " and a.ecCode like :ecCode" );
            paramMap.put("ecCode", "%"+ecCode+"%");
        }
        if(!StringUtil.isNull(ecName)){
            sb.append( " and a.ecName like :ecName" );
            paramMap.put("ecName", "%"+ecName+"%");
        }
        sb.append(" order by a.ecCode desc" );
		return (List<VwDataxEnterpriseVO>)super.queryListByPage("a.ecCode", sb.toString(), paramMap, pageInfoBean);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<VwDataxEnterpriseVO> findForAutoComplete(String ecCode,
			PageInfoBean pageInfoBean) {
		final Map<String, Object> paramMap = new LinkedHashMap<String,Object>();
        final StringBuffer sb = new StringBuffer(500);
        
        sb.append(" select a from VwDataxEnterpriseVO a where 1>0 " );
        if(!StringUtil.isNull(ecCode)){
            sb.append( " and upper(a.ecCode) like :ecCode " );
            paramMap.put("ecCode", "%"+ecCode.toUpperCase()+"%");
        }
        sb.append(" and exists (select 1 from DataxEnterpriseChannelVO t, DataxDictionaryVO d where t.id.fcEntcode = a.ecCode and d.id.fiCatagory=15 and d.id.fcCode=t.id.fcServicecode )");
        sb.append(" order by a.ecCode desc" );
		return (List<VwDataxEnterpriseVO>)super.queryListByPage("a.ecCode", sb.toString(), paramMap, pageInfoBean);
	}

}
